﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ELab.Controls.flatstyle;

namespace ELab.Controls
{
    public partial class DataGridViewStyle : DataGridView
    {
        private int ColumnIndex = 0;
        private bool ReadColumn = false;

        public DataGridViewStyle()
        {
            InitializeComponent();
            borderDrawer.BorderColor = Color.FromArgb(181, 174, 132);
        }

        private BorderDrawer borderDrawer = new BorderDrawer();
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);
            try
            {
                borderDrawer.DrawBorder(ref m, this.Width, this.Height);
            }
            catch { }
        }

        /// <summary>
        /// Color of object's border
        /// </summary>
        public Color BorderColor
        {
            get { return borderDrawer.BorderColor; }
            set
            {
                borderDrawer.BorderColor = value;
                Invalidate();
            }
        }

        #region Finding cell
        public bool Find(int ColIndex, string CellText)
        {
            bool result = false;
            if (ColIndex >= this.ColumnCount) return result;
            if (this.RowCount == 0) return result;
            if (string.IsNullOrEmpty(CellText)) return result;
            for (int i = 0; i < this.RowCount; i++)
            {
                if (this[ColIndex, i].Value == null) continue;
                if (this[ColIndex, i].Value.ToString().ToUpper() == CellText.ToUpper())
                {
                    if (!ReadColumn)
                    {
                        for (int j = 0; j < this.ColumnCount; j++)
                        {
                            if (!this.Columns[j].Visible) continue;
                            else { ReadColumn = true; ColumnIndex = j; break; }
                        }
                    }

                    this.CurrentCell = this[ColumnIndex, i];
                    this.Rows[i].Selected = true;
                    result = true;
                    break;
                }
            }
            return result;
        }

        public bool Find(string ColName, string CellText)
        {
            if (this.RowCount == 0) return false;
            int ColIndex = -1;
            for (int i = 0; i < this.ColumnCount; i++)
            {
                if (this.Columns[i].Name.ToString().ToUpper() == ColName.ToUpper())
                {
                    ColIndex = i;
                    return Find(ColIndex, CellText);
                }
            }
            return false;
        }

        public bool Find(DataGridViewColumn Column, string CellText)
        {
            return Find(Column.Index, CellText);
        }
        #endregion

        [Browsable(false)]
        public override ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return base.ContextMenuStrip;
            }
            set
            {
                base.ContextMenuStrip = value;
            }
        }

        [DefaultValue(null)]
        public ContextMenuStrip ContexMenuRows { get; set; }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            // Load context menu on right mouse click
            DataGridView.HitTestInfo hitTestInfo;
            if (e.Button == MouseButtons.Right)
            {
                hitTestInfo = this.HitTest(e.X, e.Y);
                // If column is first column
                if ((hitTestInfo.Type == DataGridViewHitTestType.Cell || hitTestInfo.Type == DataGridViewHitTestType.RowHeader) && this.ContexMenuRows != null)
                {
                    if (hitTestInfo.ColumnIndex == -1)
                    {
                        if (!ReadColumn)
                        {
                            for (int i = 0; i < this.ColumnCount; i++)
                            {
                                if (!this.Columns[i].Visible) continue;
                                else { ReadColumn = true; ColumnIndex = i; break; }
                            }
                        }
                        this.CurrentCell = this[ColumnIndex, hitTestInfo.RowIndex];
                        this.Rows[hitTestInfo.RowIndex].Selected = true;
                    }
                    else { this.CurrentCell = this[hitTestInfo.ColumnIndex, hitTestInfo.RowIndex]; this.Rows[hitTestInfo.RowIndex].Selected = true; }
                    ContexMenuRows.Tag = this.Rows[hitTestInfo.RowIndex];
                    ContexMenuRows.Show(this, new Point(e.X, e.Y));
                }
            }
        }

        private bool isSelectecStyle = false;
        [DefaultValue(false)]
        public bool IsSelectecStyle
        {
            get { return isSelectecStyle; }
            set { isSelectecStyle = value; }
        }

        private bool isAlternatingRowStyle = false;
        [DefaultValue(false)]
        public bool IsAlternatingRowStyle
        {
            get { return isAlternatingRowStyle; }
            set { isAlternatingRowStyle = value; }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                Color colorGridLine = Color.FromArgb(181, 174, 132);
                this.GridColor = colorGridLine;
                if (e.RowIndex == -1 | e.ColumnIndex == -1)
                {
                    int rect1Height = e.CellBounds.Height; //Convert.ToInt32(e.CellBounds.Height * 0.55);
                    int rect2Height = e.CellBounds.Height - rect1Height;
                    Rectangle rect1 = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, rect1Height + 1);
                    Rectangle rect2 = new Rectangle(e.CellBounds.X, e.CellBounds.Y + rect1Height, e.CellBounds.Width, rect2Height);

                    //System.Drawing.Drawing2D.LinearGradientBrush backbrush1 = new System.Drawing.Drawing2D.LinearGradientBrush(rect1, Color.FromArgb(227, 239, 255), Color.FromArgb(203, 225, 252), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                    //System.Drawing.Drawing2D.LinearGradientBrush backbrush2 = new System.Drawing.Drawing2D.LinearGradientBrush(rect2, Color.FromArgb(197, 220, 250), Color.FromArgb(135, 173, 228), System.Drawing.Drawing2D.LinearGradientMode.Vertical);

                    Color FirstGradientColor = Color.SteelBlue;
                    Color SecondGradientColor = Color.LightSteelBlue;
                    //System.Drawing.Drawing2D.LinearGradientBrush backbrush1 = new System.Drawing.Drawing2D.LinearGradientBrush(rect1, FirstGradientColor, SecondGradientColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                    SolidBrush backbrush1 = new SolidBrush(Color.FromArgb(247, 243, 222));
                    SolidBrush backbrush2 = new SolidBrush(Color.FromArgb(197, 220, 250));
                    e.Graphics.FillRectangle(backbrush2, rect2);
                    e.Graphics.FillRectangle(backbrush1, rect1);

                    Point cursorPosition = this.PointToClient(Cursor.Position);
                    if (e.CellBounds.Contains(cursorPosition) || (e.RowIndex > -1 && this.Rows[e.RowIndex].Selected))
                    {
                        SolidBrush brush = new SolidBrush(Color.FromArgb(206, 207, 181));
                        //System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(e.CellBounds, Color.FromArgb(253, 255, 255), Color.FromArgb(185, 218, 251), System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal);
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        //Pen uLinePen = new Pen(Color.Orange, 2);
                        //e.Graphics.DrawLine(uLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 2, e.CellBounds.Right - 1, e.CellBounds.Bottom - 2);
                    }

                    SolidBrush gridBrush = new SolidBrush(this.GridColor);
                    Pen gridLinePen = new Pen(new SolidBrush(colorGridLine));
                    Rectangle newRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 4, e.CellBounds.Height - 4);
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                    e.CellStyle.Font = new Font(this.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold);
                    e.PaintContent(e.CellBounds);
                    e.Handled = true;
                    //this.SelectedRows.Count > 0 AndAlso e.RowIndex = this.SelectedRows(0).Index Then
                }
                else
                {
                    if (IsAlternatingRowStyle)
                    {
                        if (e.RowIndex % 2 != 0 && !this.Rows[e.RowIndex].Selected)
                        {
                            SolidBrush backbrush1 = new SolidBrush(Color.FromArgb(247, 243, 222));
                            //System.Drawing.Drawing2D.LinearGradientBrush backbrush1 = new System.Drawing.Drawing2D.LinearGradientBrush(e.CellBounds, Color.FromArgb(227, 239, 255), Color.FromArgb(203, 225, 252), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                            e.Graphics.FillRectangle(backbrush1, e.CellBounds);

                            //SolidBrush gridBrush = new SolidBrush(this.GridColor);
                            Pen gridLinePen = new Pen(new SolidBrush(colorGridLine));
                            //New SolidBrush(Color.FromArgb(59, 97, 156)))

                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                            e.CellStyle.SelectionForeColor = Color.Black;
                            e.PaintContent(e.CellBounds);

                            e.Handled = true;
                        }
                    }

                    if (this.Rows[e.RowIndex].Selected)
                    {
                        if (IsSelectecStyle)
                        {
                            System.Drawing.Drawing2D.LinearGradientBrush backbrush1 = new System.Drawing.Drawing2D.LinearGradientBrush(e.CellBounds, Color.FromArgb(227, 239, 255), Color.FromArgb(203, 225, 252), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                            e.Graphics.FillRectangle(backbrush1, e.CellBounds);

                            //SolidBrush gridBrush = new SolidBrush(this.GridColor);
                            Pen gridLinePen = new Pen(new SolidBrush(colorGridLine));
                            //New SolidBrush(Color.FromArgb(59, 97, 156)))

                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                            e.CellStyle.SelectionForeColor = Color.Black;
                            e.PaintContent(e.CellBounds);

                            e.Handled = true;
                        }
                    }
                }

                if ((e.RowIndex < -1) || (e.RowIndex >= this.RowCount))
                {
                    base.OnCellPainting(e);
                    return;
                }
            }
            catch { }
            base.OnCellPainting(e);
        }
    }
}
